package cn.xdf.handler;

import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@ControllerAdvice//标注当前类是一个全局异常处理类
//@ResponseBody//这里return变成字符串,主要是响应对象
//
//@RestControllerAdvice
public class MyExceptionHandler02 {
    /**
     * 算数异常
     * @param model
     * @param e
     * @return
     */
    @ExceptionHandler(value = ArithmeticException.class)
    public String arithmeticExceptionHandler(Model model, Exception e){
        model.addAttribute("message",e.getMessage());
        return "forward:/error.jsp";
    }

    /**
     * 类型转化异常
     * @param model
     * @param e
     * @return
     */
    @ExceptionHandler(value = ClassCastException.class)
    public String classCastExceptionHandler(Model model,Exception e){
        model.addAttribute("message",e.getMessage());
        return "forward:/error.jsp";
    }

    /**
     * 大异常
     * @param model
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(Model model,Exception e){
        model.addAttribute("message",e.getMessage());
        return "forward:/error.jsp";
    }



}
